package com.example.hamilton_colors_lab;

/**
 * hsv[0] = Hue
 * hsv[1] = Saturation
 * hsv[2] = Value
 * 
 * Part #2 Add four buttons to the color sample application you 
 * developed last week. These buttons should, respectively: make
 * the color darker; make the color lighter; make the color more 
 * saturated, make the color less saturated. Clicking the button
 * should cause the three sliders to change as appropriate.
 */

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		TextChangeListener tcl = new TextChangeListener();
		SeekBarListener sbl = new SeekBarListener();
		moreSatListener dhbl = new moreSatListener();
		lessSatListener lhbl = new lessSatListener();
		moreValueListener drbl = new moreValueListener();
		lessValueListener lrbl = new lessValueListener();

		SeekBar redSB = (SeekBar) findViewById(R.id.redSeekbar);
		SeekBar greenSB = (SeekBar) findViewById(R.id.greenSeekbar);
		SeekBar blueSB = (SeekBar) findViewById(R.id.blueSeekbar);

		EditText redSBtext = (EditText) findViewById(R.id.redET);
		EditText greenSBtext = (EditText) findViewById(R.id.greenET);
		EditText blueSBtext = (EditText) findViewById(R.id.blueET);

		Button darkRgbButton = (Button) findViewById(R.id.darkerRGB);
		Button lightRgbButton = (Button) findViewById(R.id.lighterRGB);
		Button darkHsvButton = (Button) findViewById(R.id.darkerHSV);
		Button lightHsvButton = (Button) findViewById(R.id.lighterHSV);

		redSB.setOnSeekBarChangeListener(sbl);
		greenSB.setOnSeekBarChangeListener(sbl);
		blueSB.setOnSeekBarChangeListener(sbl);

		redSBtext.setOnClickListener(tcl);
		greenSBtext.setOnClickListener(tcl);
		blueSBtext.setOnClickListener(tcl);

		darkRgbButton.setOnClickListener(drbl);
		lightRgbButton.setOnClickListener(lrbl);
		darkHsvButton.setOnClickListener(dhbl);
		lightHsvButton.setOnClickListener(lhbl);

		redSB.setMax(255);
		greenSB.setMax(255);
		blueSB.setMax(255);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private class SeekBarListener implements OnSeekBarChangeListener {

		@Override
		public void onProgressChanged(SeekBar v, int progress, boolean isUser) {

			SeekBar redSB = (SeekBar) findViewById(R.id.redSeekbar);
			SeekBar greenSB = (SeekBar) findViewById(R.id.greenSeekbar);
			SeekBar blueSB = (SeekBar) findViewById(R.id.blueSeekbar);

			EditText redSBtext = (EditText) findViewById(R.id.redET);
			EditText greenSBtext = (EditText) findViewById(R.id.greenET);
			EditText blueSBtext = (EditText) findViewById(R.id.blueET);

			TextView hex = (TextView) findViewById(R.id.hexTextView);
			TextView hue = (TextView) findViewById(R.id.hueTextView);
			TextView sat = (TextView) findViewById(R.id.satTextView);
			TextView val = (TextView) findViewById(R.id.valueTextView);

			int redNum = redSB.getProgress();
			int greenNum = greenSB.getProgress();
			int blueNum = blueSB.getProgress();

			redSBtext.setText(Integer.toString(redNum));
			blueSBtext.setText(Integer.toString(blueNum));
			greenSBtext.setText(Integer.toString(greenNum));

			TextView colorText = (TextView) findViewById(R.id.colorText);

			String redHex = Integer.toHexString(redNum);
			String greenHex = Integer.toHexString(greenNum);
			String blueHex = Integer.toHexString(blueNum);

			hex.setText("#" + redHex + greenHex + blueHex);

			float[] hsv = new float[3];
			Color.RGBToHSV(redNum, greenNum, blueNum, hsv);
			hue.setText(Float.toString(hsv[0]));
			sat.setText(Float.toString(hsv[1]));
			val.setText(Float.toString(hsv[2]));

			colorText.setBackgroundColor(Color.rgb(redNum, greenNum, blueNum));
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}

		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}
	}

	private class TextChangeListener implements OnClickListener {

		@Override
		public void onClick(View v) {

			int y = 255;
			int x = 0;

			SeekBar redSB = (SeekBar) findViewById(R.id.redSeekbar);
			SeekBar greenSB = (SeekBar) findViewById(R.id.greenSeekbar);
			SeekBar blueSB = (SeekBar) findViewById(R.id.blueSeekbar);

			EditText redSBtext = (EditText) findViewById(R.id.redET);
			EditText greenSBtext = (EditText) findViewById(R.id.greenET);
			EditText blueSBtext = (EditText) findViewById(R.id.blueET);

			int redProgress = Integer.parseInt(redSBtext.getText().toString());
			int greenProgress = Integer.parseInt(greenSBtext.getText()
					.toString());
			int blueProgress = Integer
					.parseInt(blueSBtext.getText().toString());

			if (redProgress >= y) {
				redSBtext.setText("" + y);
				redSB.setProgress(y);
			}
			if (redProgress <= x) {
				redSBtext.setText("" + x);
				redSB.setProgress(x);
			} else {
				redSB.setProgress(redProgress);
			}
			if (blueProgress >= y) {
				blueSBtext.setText("" + y);
				blueSB.setProgress(y);
			}
			if (blueProgress <= x) {
				blueSBtext.setText("" + x);
				blueSB.setProgress(x);
			} else {

				blueSB.setProgress(blueProgress);
			}
			if (greenProgress >= y) {
				greenSBtext.setText("" + y);
				greenSB.setProgress(y);
			}
			if (greenProgress <= x) {
				greenSBtext.setText("" + x);
				greenSB.setProgress(x);
			} else {

				greenSB.setProgress(greenProgress);
			}
		}
	}

	private class moreSatListener implements OnClickListener {
		/**
		 * To make a color darker, square the value.
		 */

		@Override
		public void onClick(View v) {
			SeekBar redSB = (SeekBar) findViewById(R.id.redSeekbar);
			SeekBar greenSB = (SeekBar) findViewById(R.id.greenSeekbar);
			SeekBar blueSB = (SeekBar) findViewById(R.id.blueSeekbar);

			EditText redSBtext = (EditText) findViewById(R.id.redET);
			EditText greenSBtext = (EditText) findViewById(R.id.greenET);
			EditText blueSBtext = (EditText) findViewById(R.id.blueET);

			int redX = Integer.parseInt(redSBtext.getText().toString());
			int greenX = Integer.parseInt(greenSBtext.getText().toString());
			int blueX = Integer.parseInt(blueSBtext.getText().toString());

			float[] hsv = new float[3];
			Color.RGBToHSV((int) redX, (int) greenX, (int) blueX, hsv);

			double x = (double) hsv[1];
			hsv[1] = (float) Math.sqrt(x);

			int c = Color.HSVToColor(hsv);
			int red = Color.red(c);
			int green = Color.green(c);
			int blue = Color.blue(c);

			redSB.setProgress(red);
			blueSB.setProgress(green);
			greenSB.setProgress(blue);
		}
	}

	private class lessSatListener implements OnClickListener {
		/**
		 * To make a color lighter, replace the value by its' square root
		 */

		@Override
		public void onClick(View v) {
			SeekBar redSB = (SeekBar) findViewById(R.id.redSeekbar);
			SeekBar greenSB = (SeekBar) findViewById(R.id.greenSeekbar);
			SeekBar blueSB = (SeekBar) findViewById(R.id.blueSeekbar);

			EditText redSBtext = (EditText) findViewById(R.id.redET);
			EditText greenSBtext = (EditText) findViewById(R.id.greenET);
			EditText blueSBtext = (EditText) findViewById(R.id.blueET);

			double redX = Integer.parseInt(redSBtext.getText().toString());
			double greenX = Integer.parseInt(greenSBtext.getText().toString());
			double blueX = Integer.parseInt(blueSBtext.getText().toString());

			float[] hsv = new float[3];
			Color.RGBToHSV((int) redX, (int) greenX, (int) blueX, hsv);

			double x = (double) hsv[1];
			hsv[1] = (float) Math.pow(x, 2);

			int c = Color.HSVToColor(hsv);
			int red = Color.red(c);
			int green = Color.green(c);
			int blue = Color.blue(c);

			redSB.setProgress(red);
			greenSB.setProgress(green);
			blueSB.setProgress(blue);
		}
	}

	private class moreValueListener implements OnClickListener {
		/**
		 * To make darker, square the 'value'
		 */
		@Override
		public void onClick(View v) {
			SeekBar redSB = (SeekBar) findViewById(R.id.redSeekbar);
			SeekBar greenSB = (SeekBar) findViewById(R.id.greenSeekbar);
			SeekBar blueSB = (SeekBar) findViewById(R.id.blueSeekbar);

			EditText redSBtext = (EditText) findViewById(R.id.redET);
			EditText greenSBtext = (EditText) findViewById(R.id.greenET);
			EditText blueSBtext = (EditText) findViewById(R.id.blueET);

			int redX = Integer.parseInt(redSBtext.getText().toString());
			int greenX = Integer.parseInt(greenSBtext.getText().toString());
			int blueX = Integer.parseInt(blueSBtext.getText().toString());

			float[] hsv = new float[3];
			Color.RGBToHSV(redX, greenX, blueX, hsv);

			double x = (double) hsv[1];
			hsv[2] = (float) Math.pow(x, 2);

			int c = Color.HSVToColor(hsv);
			int red = Color.red(c);
			int green = Color.green(c);
			int blue = Color.blue(c);

			redSB.setProgress(red);
			greenSB.setProgress(green);
			blueSB.setProgress(blue);
		}
	}

	private class lessValueListener implements OnClickListener {
		/**
		 * To make a color less saturated, square the saturation.
		 */
		@Override
		public void onClick(View v) {
			SeekBar redSB = (SeekBar) findViewById(R.id.redSeekbar);
			SeekBar greenSB = (SeekBar) findViewById(R.id.greenSeekbar);
			SeekBar blueSB = (SeekBar) findViewById(R.id.blueSeekbar);

			EditText redSBtext = (EditText) findViewById(R.id.redET);
			EditText greenSBtext = (EditText) findViewById(R.id.greenET);
			EditText blueSBtext = (EditText) findViewById(R.id.blueET);

			int redX = Integer.parseInt(redSBtext.getText().toString());
			int greenX = Integer.parseInt(greenSBtext.getText().toString());
			int blueX = Integer.parseInt(blueSBtext.getText().toString());

			float[] hsv = new float[3];
			Color.RGBToHSV(redX, greenX, blueX, hsv);

			double x = (double) hsv[2];
			hsv[2] = (float) Math.sqrt(x);

			int c = Color.HSVToColor(hsv);
			int red = Color.red(c);
			int green = Color.green(c);
			int blue = Color.blue(c);

			redSB.setProgress(red);
			greenSB.setProgress(green);
			blueSB.setProgress(blue);
		}
	}
}
